package com.thinkgem.jeesite.modules.sys.security;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import com.thinkgem.jeesite.common.utils.CustomPasswordEncoder;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;


/** 
 * Description: 告诉shiro如何验证加密密码，通过SimpleCredentialsMatcher或HashedCredentialsMatcher 
 * @Author: lj
 * @Create Date: 2017-11-14 
 */  
  
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {  
	private SystemService systemService;
	
    @Override   
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {    
    	UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    	User user = getSystemService().getUserByLoginName(token.getUsername());
    	CustomPasswordEncoder cpder = new CustomPasswordEncoder();
    	return cpder.matches(new String(token.getPassword()),user.getPassword());
    } 
	/**
	 * 获取系统业务对象
	 */
	public SystemService getSystemService() {
		if (systemService == null){
			systemService = SpringContextHolder.getBean(SystemService.class);
		}
		return systemService;
	}
}    